package com.mach.platform.sprider.getter;

import com.mach.platform.sprider.dev.virdev.*;
import com.mach.platform.sprider.getter.DataLogService;
import com.mach.platform.sprider.opc.OPCDADev;
import com.mach.platform.util.SqlHelper;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

public class DevFactory {

    protected static Map<Integer,GeneralDev> devMaps = new HashMap<Integer,GeneralDev>();

    public static GeneralDev getOrNew(Map<String, Object> devInfoMap, SqlHelper sqlHelper, DataLogService dataLogService) throws SQLException {
        Integer devId = Integer.parseInt(devInfoMap.get("devId").toString());

        Map<String, Object> devInfo = devInfoMap;

        GeneralDev dev = null;

        dev = devMaps.get(devId);
        String prol = (String)devInfo.get("prol");

        if(dev == null && prol != null){

            if("GeneralModbusRTU".equals(prol)){
                dev =  new GeneralModbusDev(devInfo, sqlHelper, dataLogService);

            }else
            if ("GeneralModbusTCP".equals(prol)){
                dev =  new GeneralModbusDev(devInfo, sqlHelper, dataLogService);
            }else
            if ("SimpleValveDevGeneralModbusTCP".equals(prol)
                    || "SimpleValveDevGeneralModbusRTU".equals(prol)){
                dev =  new SimpleValveDev(devInfo, sqlHelper, dataLogService);
            }else
            if ("SimpleSwitchDevGeneralModbusRTU".equals(prol)
                    || "SimpleSwitchDevGeneralModbusTCP".equals(prol)){
                dev =  new SimpleSwitchDev(devInfo, sqlHelper, dataLogService);
            }else
            if ("DbChannelSwitchDevGeneralModbusRTU".equals(prol)
                    || "DbChannelSwitchDevGeneralModbusTCP".equals(prol)){
                dev =  new DbChannelSwitchDev(devInfo, sqlHelper, dataLogService);
            } else
            if ("SimpleDumperStatusDevGeneralModbusRTU".equals(prol)
                    || "SimpleDumperStatusDevGeneralModbusTCP".equals(prol)){
                dev =  new SimpleDumperStatusDev(devInfo, sqlHelper, dataLogService);
            } else
            if ("TransducerCtrDevGeneralModbusRTU".equals(prol)
                    || "TransducerCtrDevGeneralModbusTCP".equals(prol)){
                dev =  TransducerCtrDev.getAndNew(devInfo, sqlHelper, dataLogService);
            } else
            if ("TempKeeperAutoCtrl".equals(prol)){
                dev = new TempKeeperAutoCtrl(devInfo, sqlHelper, dataLogService);
            } else
            if ("PressKeeperAutoCtrl".equals(prol)){
                dev = new PressKeeperAutoCtrl(devInfo, sqlHelper, dataLogService);
            }
            else
            if ("PressCompAirMainKeeperAutoCtrl".equals(prol)){
                dev = new PressCompAirMainKeeperAutoCtrl(devInfo, sqlHelper, dataLogService);
            } else
            if ("PressCompAirCoolKeeperAutoCtrl".equals(prol)){
                dev = new PressCompAirCoolKeeperAutoCtrl(devInfo, sqlHelper, dataLogService);
            }else
            if ("GernalAlamDev".equals(prol)){
                dev = new GernalAlamDev(devInfo, sqlHelper, dataLogService);
            }
            //PressCompAirMainKeeperAutoCtrl
            //PressCompAirCoolKeeperAutoCtrl
            else
            if(( prol != null ) && (prol.startsWith("GeneralS7"))){
                dev = new GeneralS7Dev(devInfo, sqlHelper, dataLogService);
            }

            if(( prol != null ) && (prol.startsWith("OPCDA"))){
                dev = OPCDADev.getOrNew(devInfo, sqlHelper, dataLogService);
            }

            if(dev !=null){
                dev.updateDevInfo(devInfo);
                devMaps.put(devId, dev);
            }else{
                System.out.println("dev is null prol=" + prol);
            }


        }

        return dev;
    }

    public static GeneralDev getById(Integer devId) {
        return devMaps.get(devId);
    }
}
